<?php

function my_encrypt($var,$salt){
     return sha1($var.$salt);
}

function purify($var){
	return trim(strip_tags(nl2br($var)));
}

function clean($var){
	$var = strip_tags($var);
	return auto_typography($var);
}

function set_url_title($var){
	$var = word_limiter($var,10,'');
	$var = strtolower($var);
	$var = trim($var);	
    return url_title($var,'dash',TRUE);
}

function show_dialog($status,$title,$message,$button,$action){
	$CI =& get_instance(); 
	$CI->session->set_flashdata('dialog','yes');
	$CI->session->set_flashdata('dialog_status',$status);	
	$CI->session->set_flashdata('dialog_title',$title);	
	$CI->session->set_flashdata('dialog_message',$message);
	$CI->session->set_flashdata('dialog_button',$button);
	$CI->session->set_flashdata('dialog_action',$action);	
}

function get_age($_dob) {
    $dob = date("Y-m-d",strtotime($_dob));
    $ageparts = explode("-",$dob);

    $age = date("Y-m-d")-$dob;

    return (date("nd") < $ageparts[1].str_pad($ageparts[2],2,'0',STR_PAD_LEFT)) ? $age-=1 : $age;
}

function time_since($original) {
	
	$original = strtotime($original);
	$chunks = array(
			array(60 * 60 * 24 * 365 , 'year'),
			array(60 * 60 * 24 * 30 , 'month'),
			array(60 * 60 * 24 * 7, 'week'),
			array(60 * 60 * 24 , 'day'),
			array(60 * 60 , 'hour'),
			array(60 , 'minute'),
	);
	
	$today = time();
	$since = $today - $original;

	if($since > 604800) {
		$print = date("M jS", $original);
		if($since > 31536000) {
			$print .= ", " . date("Y", $original);
		}
		return $print;
	}
	
	for ($i = 0, $j = count($chunks); $i < $j; $i++) {
		$seconds = $chunks[$i][0];
		$name = $chunks[$i][1];
		
		if (($count = floor($since / $seconds)) != 0) {
				break;
		}
	}

	$print = ($count == 1) ? '1 '.$name : "$count {$name}s";
	return $print . " ago";
}

function format_long_date($date){
	$y = substr($date,0,4);
	$m = substr($date,5,2);
	$d = substr($date,8,2);
	return strftime("%A, %d %B %Y", mktime(0, 0, 0, $m, $d, $y));
}

function format_short_date($date){
	$y = substr($date,0,4);
	$m = substr($date,5,2);
	$d = substr($date,8,2);
	return strftime("%d.%m.%y", mktime(0, 0, 0, $m, $d, $y));
}

function format_short_date_year($date){
	$y = substr($date,0,4);
	$m = substr($date,5,2);
	$d = substr($date,8,2);
	return strftime("%d.%m.%Y", mktime(0, 0, 0, $m, $d, $y));
}

function format_medium_date($date){
	$y = substr($date,0,4);
	$m = substr($date,5,2);
	$d = substr($date,8,2);
	return strftime("%d %B %Y", mktime(0, 0, 0, $m, $d, $y));
}

function format_hour($date){
	$h = substr($date,0,2);
	$m = substr($date,3,2);
	$s = substr($date,6,2);
	return strftime("%H:%M", mktime($h, $m, $s, 0, 0, 0));
}

function format_indonesia_date($date){
	$day = substr($date,0,1);
	$numDay = substr($date,3,2);
	$month = substr($date,6,2);
	$year = substr($date,9,4);
	
	$array_hari = array(1 =>"Senin","Selasa","Rabu","Kamis","Jumat","Sabtu","Minggu");
	$hari = $array_hari[$day];
	
	$array_bulan = array(
						'01'=>"Januari",
						'02'=>"Februari",
						'03'=>"Maret",
						'04'=>"April",
						'05'=>"Mei",
						'06'=>"Juni",
						'07'=>"Juli",
						'08'=>"Agustus",
						'09'=>"September",
						'10'=>"Oktober",
						'11'=>"November",
						'12'=>"Desember");
						
	$bulan = $array_bulan[$month];

	return $hari.', '.$numDay.' '.$bulan.' '.$year;
}

function get_ip_address(){
    if (!empty($_SERVER['HTTP_CLIENT_IP'])){
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }else{
      $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

function create_url($type,$categories_alias,$details_title,$details_id){

	switch ($type){
		case 'buzz':
				return 'categories/'.$categories_alias.'/'.$details_title.'/'.$details_id;	
			break;
			
		case 'comment':
				return 'categories/'.$categories_alias.'/'.$details_title.'/'.$details_id.'#comments';	
			break;
	}
}


function set_select_month()
{
	// Month Array
	$months = array(
		"01"=>"January",
		"02"=>"February",
		"03"=>"March",
		"04"=>"April",
		"05"=>"May",
		"06"=>"June",
		"07"=>"July",
		"08"=>"August",
		"09"=>"September",
		"10"=>"October",
		"11"=>"November",
		"12"=>"December"
	);
	
	return $months;
}

function set_select_date()
{
	$date = array();
	for($i=1; $i<=31; $i++)
	{
		if($i<10) $i = "0".$i;
		$date[] = $i;
	}	
	return $date;
}

function set_select_year($type="")
{
	$year = array();
	if($type=="latest")
	{
		$begin = date('Y')-1;
		$end = date('Y')+1;
	}
	else
	{
		$begin = 2000;
		$end = 2011;
	}
	
	for($i=$begin; $i<=$end; $i++)
	{
		$year[] = $i;
	}
	return $year;
}


function is_day_valid($d,$m,$y)
{
	if($d <> '' && $m <> '' && $y <> '')
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

function is_mobile()
{
	$mobile_browser = '0';
	 
	if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
		$mobile_browser++;
	}
	 
	if((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')>0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
		$mobile_browser++;
	}    
	 
	$mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));
	$mobile_agents = array(
		'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
		'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
		'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
		'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
		'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
		'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
		'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
		'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
		'wapr','webc','winw','winw','xda','xda-');
	 
	if(in_array($mobile_ua,$mobile_agents)) {
		$mobile_browser++;
	}
	 
	if (strpos(strtolower(@$_SERVER['ALL_HTTP']),'OperaMini')>0) {
		$mobile_browser++;
	}
	 
	if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows')>0) {
		$mobile_browser=0;
	}
	
	return $mobile_browser;
}

function notification($type,$message)
{
	switch($type)
	{
		case 'success':
			$notif = '<div class="notification notification-success">'.$message.'</div>';
			break;
		case 'error':
			$notif = '<div class="notification notification-error">'.$message.'</div>';
			break;
	}
	
	return $notif;
}

function set_url_attachment($file,$url)
{
	if($file<>'' && $url<>'')
	{
		return ' href="'.base_url().'/uploads/pages/'.$file.'" target="_blank"';
	}
	
	if($file<>'' && $url=='')
	{
		return ' href="'.base_url().'/uploads/pages/'.$file.'" target="_blank"';
	}
	
	if($file=='' && $url<>'')
	{
		return ' href="'.prep_url($url).'" target="_blank"';
	}
	
	if($file=='' && $url=='')
	{
		return '';
	}
}

function set_url_text($type)
{
	
	if($type==1)
	{
		return 'Read More &gt;&gt;';
	}
	else
	{
		return 'Download &gt;&gt;';
	}
	
}

function set_url_youtube($url)
{
	//$url = prep_url(trim($url));
	$url = str_replace('watch?v=','v/',$url);
	return $url;
}

function get_youtube_image($url='')
{
	$is_youtube = strpos($url,"youtube") ? TRUE : FALSE;

	if($url<>'' && $is_youtube){
		$url = str_replace('www','img',$url);
		$url = str_replace('v/','vi/',$url);
		$url .= '/2.jpg';
		return $url;
	}
	else
	{
		return '';
	}
	
}

function get_image($image,$youtube_image)
{

	if($image<>"" && $youtube_image<>"")
	{
		return $youtube_image;
	}
	elseif($image=="" && $youtube_image<>"")
	{
		return $youtube_image;
	}
	elseif($image<>"" && $youtube_image=="")
	{
		return base_url()."uploads/pages/".$image;
	}
	else
	{
		return NULL;
	}
}

function print_r_with_pre($vars)
{
	return '<pre>'.print_r($vars).'</pre>';
}

function image_mime_type($mime_types){
    
    $array_mime_types = array(
		'application/pdf' => 'images/format/pdf.png',
		'application/msword' => 'images/format/word.png',
		'application/zip' => 'images/format/zip.png',
		'application/vnd.ms-powerpoint' => 'images/format/ppt.png',
		
    );
	if (array_key_exists($mime_types, $array_mime_types)){
		$m= $array_mime_types[$mime_types];	
	}
	else {
		$m= 'images/format/file.png';
	}
	
	return $m;
	
    
}

?>
